package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.UserFriendDao;
import io.renren.modules.generator.entity.UserFriendEntity;
import io.renren.modules.generator.service.UserFriendService;


@Service("userFriendService")
public class UserFriendServiceImpl extends ServiceImpl<UserFriendDao, UserFriendEntity> implements UserFriendService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserFriendEntity> page = this.page(
                new Query<UserFriendEntity>().getPage(params),
                new QueryWrapper<UserFriendEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public IPage<Map<String, Object>> selectFriendList(Page<Map<String, Object>> page, Map<String, Object> params) {
        return baseMapper.selectFriendList(page, params);
    }

    @Override
    public PageUtils selectAddFriendList(Map<String, Object> params) {
        //用户的所有好友id集合
        List<Long> friendList = new ArrayList<>();
        //添加用户自己id，让自己不被搜索到
        friendList.add(ShiroUtils.getUserId());
        Long userId = ShiroUtils.getUserId();
        QueryWrapper<UserFriendEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserFriendEntity> userFriendEntities = baseMapper.selectList(wrapper);
        if (!userFriendEntities.isEmpty()) {
            for (UserFriendEntity userFriendEntity : userFriendEntities) {
                friendList.add(userFriendEntity.getFriendId());
            }
        }

        PageUtils pageUtils = sysUserService.allFriendPage(params);
        List<SysUserEntity> list = (List<SysUserEntity>) pageUtils.getList();
        if (!list.isEmpty()) {
            //如果查询到的用户存在好友列表里直接移除
            list.removeIf(sysUserEntity -> friendList.contains(sysUserEntity.getUserId()));
            pageUtils.setList(list);
        }

        return pageUtils;
    }

    /**
     * 单方面删除
     * @param friendId
     */
    @Override
    public void deleteFirend(Long friendId) {
        Long userId = ShiroUtils.getUserId();
        QueryWrapper<UserFriendEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("friend_id", friendId);
        Long id = baseMapper.selectOne(wrapper).getId();
        baseMapper.deleteById(id);
    }

}
